<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
     <h3>h3</h3>
     <input type="text">
     <input type="text" name="hehe">
     <input type="text" name="hehe">
     <div id="main">
        <div id="box1" name="hehe" index="0">div1</div>
        <div id="box" name="hehe" index="0">div2</div>
     </div>
     <div id="box" name="hehe" >div3</div>
</body>
</html>
<script>
  //document.getElementsByName("hehe")
  // 获取页面上所有的元素
 
  var omain=document.getElementById("main");
  function myName (attr,val,context){
     context=context||document;
    var allTag=context.getElementsByTagName("*");
      // 目标盒子
      var res=[];
      for(var i=0;i<allTag.length;i++){
         var item=allTag[i];
         // 满足条件的我再往盒子里面装
         if(item.getAttribute(attr)==val){
             res.push(item);
         }
      }
      return res;
  }

//   var res=myName ("name","hehe");
//   var res=myName ("index",0);//[]
//   var res=myName ("type","text");//[]
//   var res=myName("index",0,omain);
  var res=myName("index",0);

</script>

